package cn.iocoder.yudao.module.reading.controller.app.bookset;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.reading.controller.app.bookset.vo.BookSetDetailRespVO;
import cn.iocoder.yudao.module.reading.controller.app.bookset.vo.BookSetPageReqVO;
import cn.iocoder.yudao.module.reading.controller.app.bookset.vo.BookSetRespVO;
import cn.iocoder.yudao.module.reading.convert.bookset.BookSetConvert;
import cn.iocoder.yudao.module.reading.dal.dataobject.bookset.BookSetDO;
import cn.iocoder.yudao.module.reading.service.bookset.BookSetService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * 用户 APP - 绘本集
 *
 * @author 芋道源码
 */
@Tag(name = "用户 APP - 绘本集")
@RestController
@RequestMapping("/reading/book-sets")
@Validated
@Slf4j
public class BookSetController {

    @Resource
    private BookSetService bookSetService;

    @GetMapping("/page")
    @Operation(summary = "获得绘本集分页")
    @PreAuthenticated
    public CommonResult<PageResult<BookSetRespVO>> getBookSetPage(@Valid BookSetPageReqVO pageReqVO) {
        Long userId = getLoginUserId();
        PageResult<BookSetDO> pageResult = bookSetService.getBookSetPage(pageReqVO, userId);
        return success(BookSetConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/{id}")
    @Operation(summary = "获得绘本集详情")
    @PreAuthenticated
    public CommonResult<BookSetDetailRespVO> getBookSet(@Parameter(description = "绘本集编号", required = true, example = "1024") @PathVariable("id") Long id) {
        Long userId = getLoginUserId();
        BookSetDetailRespVO result = bookSetService.getBookSetDetail(id, userId);
        return success(result);
    }

    @GetMapping("/recommended")
    @Operation(summary = "获得推荐绘本集")
    @PreAuthenticated
    public CommonResult<List<BookSetRespVO>> getRecommendedBookSets(
            @Parameter(description = "语言类型", example = "en") @RequestParam(value = "lang", required = false) String lang,
            @Parameter(description = "分类ID", example = "2") @RequestParam(value = "categoryId", required = false) Long categoryId,
            @Parameter(description = "限制数量", example = "10") @RequestParam(value = "limit", required = false) Integer limit) {
        Long userId = getLoginUserId();
        List<BookSetDO> list = bookSetService.getRecommendedBookSets(lang, categoryId, limit, userId);
        return success(BookSetConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/search")
    @Operation(summary = "搜索绘本集")
    @PreAuthenticated
    public CommonResult<PageResult<BookSetRespVO>> searchBookSets(
            @Parameter(description = "搜索关键词", required = true, example = "牛津") @RequestParam("keyword") String keyword,
            @Parameter(description = "语言类型", example = "en") @RequestParam(value = "lang", required = false) String lang,
            @Parameter(description = "页码", example = "1") @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
            @Parameter(description = "每页数量", example = "20") @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize) {
        Long userId = getLoginUserId();
        PageResult<BookSetDO> pageResult = bookSetService.searchBookSets(keyword, lang, pageNo, pageSize, userId);
        return success(BookSetConvert.INSTANCE.convertPage(pageResult));
    }

}
